/* eslint-disable max-lines */
import {
  type Slide,
  type PPTElement,
  type PPTTextElement,
  type PPTImageElement,
} from '@/types/slides'
import defaultImage from '@/assets/image/Default.png'
import { nanoid } from 'nanoid'

const EQUIP_FIXED_KEY = {
  设备名称: 'equipName',
  设备图片: 'equipImage',
  设备介绍: 'equipDesc',
  设备品牌: 'equipBrand',
  设备编码: 'equipCode',
  设备型号: 'equipModel',
  设备单价: 'equipPrice',
  库存数量: 'equipStock',
  性能参数: 'equipPerformance',
} as const

type FixedKeyType = keyof typeof EQUIP_FIXED_KEY

interface ISystemElementOptions {
  left: number
  top: number
  width?: number
  height?: number
}

function getEquipFixedKey(key: FixedKeyType): string {
  return EQUIP_FIXED_KEY[key]
}

const logoElement = ({ left, top }: ISystemElementOptions): PPTImageElement => {
  return {
    type: 'image',
    id: nanoid(10),
    src: '',
    width: 161,
    height: 35,
    left: left || 824,
    top: top || 13.5,
    fixedRatio: true,
    rotate: 0,
  }
}

const equipImageElement = (
  key: string,
  { left, top, width, height }: ISystemElementOptions,
): PPTImageElement => {
  return {
    type: 'image',
    id: nanoid(10),
    src: defaultImage,
    replaceSrc: `{{${key}}}`, // 标识
    width: width || 303,
    height: height || 234,
    left: left || 0,
    top: top || 0,
    fixedRatio: true,
    rotate: 0,
  }
}

const equipTitleElement = (
  key: string,
  { left, top }: ISystemElementOptions,
): PPTTextElement => {
  return {
    type: 'text',
    id: nanoid(10),
    left: left || 303,
    top: top || 0,
    width: 680,
    height: 80,
    content: `<p style=""><strong><span style="font-size: 28px;">{{${key}}}</span></strong></p>`,
    rotate: 0,
    defaultFontName: 'Microsoft Yahei',
    defaultColor: '#333',
    vertical: false,
  }
}

const equipDescElement = (
  key: string,
  {
    aiValue = '',
    left,
    top,
    width,
    height,
  }: {
    aiValue?: string
    left?: number
    top?: number
    width?: number
    height?: number
  } = {},
): PPTTextElement => {
  return {
    type: 'text',
    id: nanoid(10),
    left: left || 303,
    top: top || 69,
    width: width || 680,
    height: height || 50,
    content: `<p style=""><strong><span style="font-size: 18px;">{{${key}}}</span></strong></p>`,
    rotate: 0,
    defaultFontName: 'Microsoft Yahei',
    defaultColor: '#333',
    vertical: false,
    aiValue,
  }
}

/**
 * 设备自定义字段元素
 */
const equipFieldsTextElement = (fixedKeyArr: string[]): PPTTextElement => {
  let content = `<ul style="list-style-type: disc; font-size: 14px; color: #333">${fixedKeyArr
    .map((key) => {
      return `<li><p style="">{{${key}}}</p></li>`
    })
    .join('')}</ul>`
  if (!fixedKeyArr.length) {
    content = ''
  }
  return {
    type: 'text',
    id: nanoid(10),
    left: 320,
    top: 210,
    width: 680,
    height: 156,
    rotate: 0,
    defaultFontName: 'Microsoft Yahei',
    defaultColor: '#333',
    vertical: false,
    content: content,
  }
}

/**
 * 设备表头
 */
const equipHeader = (): PPTElement[] => [
  {
    type: 'text',
    id: nanoid(10),
    left: 15,
    top: -8.881784197001252e-16,
    width: 201.8367140982945,
    height: 62,
    content:
      '<p style=""><strong><span style="font-size: 28px;">设备介绍</span></strong></p>',
    rotate: 0,
    defaultFontName: 'Microsoft Yahei',
    defaultColor: '#333',
    vertical: false,
  },
  {
    type: 'image',
    id: nanoid(10),
    src: '',
    width: 161,
    height: 35,
    left: 824,
    top: 13.5,
    fixedRatio: true,
    rotate: 0,
  },
  {
    type: 'line',
    id: nanoid(10),
    left: 24.674538298516488,
    top: 62,
    start: [0, 0],
    end: [956.7060248259158, 0],
    points: ['', ''],
    color: '#5b9bd5',
    style: 'solid',
    width: 2,
  },
]

/**
 * 生成设备默认布局
 */
const genDefaultEquipLayout = (fixedKeyArr: string[]): Slide => {
  const header = equipHeader()
  const image = equipImageElement(getEquipFixedKey('设备图片'), {
    left: 17,
    top: 69,
  })
  const title = equipTitleElement(getEquipFixedKey('设备名称'), {
    left: 320,
    top: 63,
  })
  const desc = equipDescElement(getEquipFixedKey('设备介绍'), {
    left: 320,
    top: 124,
  })
  const fields = equipFieldsTextElement(fixedKeyArr)

  const page: Slide = {
    id: nanoid(10),
    elements: [...header, image, title, desc, fields],
    background: {
      type: 'solid',
      color: '#ffffff',
    },
  }

  return page
}

export {
  EQUIP_FIXED_KEY,
  getEquipFixedKey,
  genDefaultEquipLayout,
  equipImageElement,
  equipTitleElement,
  equipDescElement,
  equipFieldsTextElement,
  equipHeader,
  logoElement,
}
